package com.project.weimi.activity;

import java.util.ArrayList;
import java.util.List;

import com.project.weimi.R;
import com.project.weimi.adapter.AddressManagerAdapter;
import com.project.weimi.entity.response.AddressEntity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AddressManagerActivity extends BaseActivity implements OnRefreshListener {

	private LinearLayout empty_ll;
	private TextView address_new;
	
	private SwipeRefreshLayout mSwipeRefreshWidget;
	private LinearLayoutManager mLayoutManager;
	private RecyclerView mRecyclerView;
	
	private AddressManagerAdapter mAdapter;
	private int lastVisibleItem;
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				mSwipeRefreshWidget.setRefreshing(false);
				mAdapter.getList().clear();
				addList();
				break;
			case 1:
				addList();
				break;
			default:
				break;
			}
		}
	};
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentViewWithBar(R.layout.activity_addressmanager, "地址管理");
		empty_ll = (LinearLayout) this.findViewById(R.id.empty_ll);
		address_new = (TextView) this.findViewById(R.id.address_new);
		
		initView();
	}
	
	private void initView() {
		mSwipeRefreshWidget = (SwipeRefreshLayout) this.findViewById(R.id.swipe_refresh_widget);
		mRecyclerView = (RecyclerView) this.findViewById(android.R.id.list);

		mSwipeRefreshWidget.setColorScheme(R.color.color1, R.color.color2, R.color.color3, R.color.color4);
		mSwipeRefreshWidget.setOnRefreshListener(this);
		

		mRecyclerView.setHasFixedSize(true);
		mLayoutManager = new LinearLayoutManager(this);
		mRecyclerView.setLayoutManager(mLayoutManager);
		mRecyclerView.setItemAnimator(new DefaultItemAnimator());

		mAdapter = new AddressManagerAdapter(this);
		mRecyclerView.setAdapter(mAdapter);
//		addList();
		
		mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
			@Override
			public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
				super.onScrollStateChanged(recyclerView, newState);
				
				if(mAdapter.getItemCount() <= 3){
					return;
				}
				
				if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItem + 1 == mAdapter.getItemCount()) {
					handler.sendEmptyMessageDelayed(1, 3000);
				}
			}

			@Override
			public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
				super.onScrolled(recyclerView, dx, dy);
				lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
			}

		});

		if(mAdapter.getList() == null || mAdapter.getList().size() <= 0){
			mRecyclerView.setVisibility(View.GONE);
			empty_ll.setVisibility(View.VISIBLE);
		}
		
		empty_ll.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				addList();
			}
		});
		address_new.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(AddressManagerActivity.this, AddressManagerEditorActivity.class);
				Bundle bundle = new Bundle();
				bundle.putSerializable("address", null);
				bundle.putInt("states", 2); // 新增
				bundle.putInt("position", -1); // position
				intent.putExtras(bundle);
				startActivityForResult(intent, 2);
			}
		});
	}
	/** 数据适配 */
	private void addList() {
		empty_ll.setVisibility(View.GONE);
		mRecyclerView.setVisibility(View.VISIBLE);
		
		List<AddressEntity> list = getList();
		mAdapter.getList().addAll(list);
		mAdapter.notifyDataSetChanged();
	}
	
	/** 获取数据 */
	private List<AddressEntity> getList() {
		List<AddressEntity> list = new ArrayList<AddressEntity>();
		AddressEntity shopEntity = null;
		int len = mAdapter.getList().size();
		for (int i = (len + 1); i < (len + 10); i++) {
			if(i == 3){
				shopEntity = new AddressEntity(true, "北京，北京市，昌平区，", "东小口，", "双营路美立方小区4号楼一单元外底商117二层", "1810673758" + (i % 3), "我的地址" + (i % 3), "20220" + (i % 3));
			} else {
				shopEntity = new AddressEntity(false, "北京，北京市，昌平区，", "东小口，", "双营路美立方小区4号楼一单元外底商117二层", "1810673758" + (i % 3), "我的地址" + (i % 3), "20220" + (i % 3));
			}

			list.add(shopEntity);
		}
		return list;
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		handler.sendEmptyMessageDelayed(0, 3000);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, intent);
		
		if(intent == null)
			return;
		
		Bundle bundle = intent.getExtras();
		AddressEntity addressEntity = (AddressEntity) bundle.getSerializable("addres");
		
		if(requestCode == 1){
			int position = bundle.getInt("position");
			mAdapter.getList().get(position).setAddress_name(addressEntity.getAddress_name());
			mAdapter.getList().get(position).setAddress_phone(addressEntity.getAddress_phone());
			mAdapter.getList().get(position).setAddress_area(addressEntity.getAddress_area());
			mAdapter.getList().get(position).setAddress_jiedao(addressEntity.getAddress_jiedao());
			mAdapter.getList().get(position).setAddress_youbian(addressEntity.getAddress_youbian());
			mAdapter.getList().get(position).setAddress_address(addressEntity.getAddress_address());
			mAdapter.getList().get(position).setAddress_check(addressEntity.isAddress_check());
			
		} else if(requestCode == 2) {
			if(mAdapter.getList() == null || mAdapter.getList().size() <= 0){
				mRecyclerView.setVisibility(View.GONE);
				empty_ll.setVisibility(View.VISIBLE);
			} else if (empty_ll.isShown()) {
				mRecyclerView.setVisibility(View.VISIBLE);
				empty_ll.setVisibility(View.GONE);
			}
			
			mAdapter.getList().add(0, addressEntity);
		}
		mAdapter.notifyDataSetChanged();
	}
	
	@Override
	void setStatusBar(int statusBarColorRes) {
		// TODO Auto-generated method stub
		super.setStatusBar(R.color.transparent_3);
	}
	
}
